﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KendoMvcUI.Common;

namespace KendoMvcUI.UI.Grid
{
    /// <summary>
    /// This property, which describes the model of the subgrid, has an effect only if the subGrid property is set to true. It is an array in which we describe the column model for the subgrid data.
    /// </summary>
    public class SubGridModelSettings
    {
        public SubGridModelSettings()
        {
            SubGridModels = new List<SubGridModel>();
        }
        public IEnumerable<SubGridModel> SubGridModels { get; set; }

        public override string ToString()
        {
            StringBuilder sbName = new StringBuilder();
            StringBuilder sbWidth = new StringBuilder();
            StringBuilder sbAlign = new StringBuilder();
            StringBuilder sbParams = new StringBuilder();
            StringBuilder sbMapping = new StringBuilder();
            if(SubGridModels==null||SubGridModels.Count()==0)
                return "";

            foreach (var subGridModel in SubGridModels)
            {
                sbAlign.Append("'{0}',".Formator(subGridModel.Align == null ? "left" : subGridModel.Align));
                sbWidth.Append("{0},".Formator(subGridModel.Width));
                if(!subGridModel.Params.IsNullOrWhiteSpace())
                {
                    sbParams.Append("{0},".Formator(subGridModel.Params));
                }
                
                sbName.Append("'{0}',".Formator(subGridModel.Name));
                sbMapping.Append("'{0}',".Formator(subGridModel.Mapping.IsNullOrWhiteSpace()?subGridModel.Name:subGridModel.Mapping));
            }

            string subGridModelStr = "";
            if (sbName.Length > 0)
                subGridModelStr += "name:[{0}],".Formator(sbName.ToString().TrimEnd(','));
            if (sbWidth.Length > 0)
                subGridModelStr += "width:[{0}],".Formator(sbWidth.ToString().TrimEnd(','));
            if (sbAlign.Length > 0)
                subGridModelStr += "align:[{0}],".Formator(sbAlign.ToString().TrimEnd(','));
            if (sbParams.Length > 0)
                subGridModelStr += "params:[{0}],".Formator(sbParams.ToString().TrimEnd(','));
            if (sbMapping.Length > 0)
                subGridModelStr += "mapping:[{0}],".Formator(sbMapping.ToString().TrimEnd(','));

            return "{"+subGridModelStr.TrimEnd(',')+"}";
        }
        private void StringHelper(StringBuilder sb,string property,string value)
        {
            sb.Append("{");
            sb.Append(property+":");
            sb.Append("}");
        }
    }
    public class SubGridModel
    {
        public SubGridModel()
        {
            Width = 120;
        }
        /// <summary>
        /// an array containing the labels of the columns of the subgrid.
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// an array containing the width of the columns. This array should have the same length as in name array.
        /// </summary>
        public int Width { get; set; }
        /// <summary>
        /// an array containing the alignment of the columns. The values can be left, center or right. If omited the aliment is left. 
        /// </summary>
        public string Align { get; set; }
        /// <summary>
        /// an array in which we can add a name from main grid's colModel to pass as additional parameter to the subGridUrl. 
        /// </summary>
        public string Params { get; set; }
        /// <summary>
        /// the parameter is used only when repeatitems in subgrid is set to false. 
        /// When defined in this case we use the names from this array to map it to the subgrid names. 
        /// If not set and repeatitems is false we use the name option.
        /// </summary>
        public string Mapping { get; set; }
    }
}
